//
//  TopNewsTableViewCell.m
//  NBNews
//
//  Created by lanou3g on 14-10-8.
//  Copyright (c) 2014年 Hero. All rights reserved.
//

#import "TopNewsTableViewCell.h"
#import "UIImageView+WebCache.h"


@implementation TopNewsTableViewCell
- (void)dealloc
{
    [_titleLabel release];
    [_newsImageView release];
    [_digestLabel release];
    [_secondImageView release];
    [_thirdImageView release];
    [_scrollView release];
    [_pageControl release];
    [_timer release];
    [_topNews release];
    [_bottomLine release];
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //背景色
        self.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1.0];
        //标题
        UILabel * titleLabel = [[UILabel alloc]init];
        self.titleLabel = titleLabel;
        [self.contentView addSubview:titleLabel];
        [titleLabel release];
        //摘要
        UILabel * digestLabel = [[UILabel alloc]init];
        self.digestLabel = digestLabel;
        digestLabel.textColor = [UIColor darkGrayColor];
        [self.contentView addSubview:digestLabel];
        [digestLabel release];
        //图片
        UIImageView * newsImageView = [[UIImageView alloc]init];
        self.newsImageView = newsImageView;
        [self.contentView addSubview:newsImageView];
        [newsImageView release];
        //图集中第二张图片
        UIImageView * secondImageView = [[UIImageView alloc]init];
        self.secondImageView = secondImageView;
        [self.contentView addSubview:secondImageView];
        [secondImageView release];
        //图集中第三张图片
        UIImageView * thirdImageView = [[UIImageView alloc]init];
        self.thirdImageView = thirdImageView;
        [self.contentView addSubview:thirdImageView];
        [thirdImageView release];
        //底部的线
        UIView * bottomLine = [[UIView alloc]init];
        self.bottomLine = bottomLine;
        [self.contentView addSubview:bottomLine];
        [bottomLine release];
    }
    return self;
}
- (void)setTopNews:(TopNews *)topNews
{
    
 
    if (topNews.number == 0) {
        
        //第一张图
        self.newsImageView.frame = CGRectMake(0,0, 320, 185);
        [self.newsImageView sd_setImageWithURL:[NSURL URLWithString:topNews.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"] options:SDWebImageProgressiveDownload];
        //标题
        self.titleLabel.frame =CGRectMake(10, CGRectGetMaxY(self.newsImageView.frame)+5, 190,20);
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        self.titleLabel.text = topNews.title;
        
        
//        //设置UIScrollview
//        UIScrollView * scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320,210)];
//        self.scrollView = scrollView;
//        [self.contentView addSubview:scrollView];
//        [scrollView release];
//        //设置UIPageControl
//        UIPageControl * page = [[UIPageControl alloc]initWithFrame:CGRectMake(250,190,60, 20)];
//        self.pageControl = page;
//        page.numberOfPages = self.scrollImageArray.count;
//        page.currentPageIndicatorTintColor = [UIColor blackColor];
//        page.pageIndicatorTintColor = [UIColor lightGrayColor];
//        [self.contentView addSubview:page];
//        [page release];
//        //设置图片的宽,高,Y,图片数
//        CGFloat imageW = self.scrollView.frame.size.width;
//        CGFloat imageH = self.scrollView.frame.size.height-25;
//        CGFloat imageY = 0;
//        NSInteger totalCount = page.numberOfPages;
//        for (int i = 0; i < self.scrollImageArray.count; i++) {
//            //图片X
//            CGFloat imageX = i * imageW;
//            ScrollViewModel * model = [self.scrollImageArray objectAtIndex:i];
//            SDWebImageManager * manager = [SDWebImageManager sharedManager];
//            [manager downloadImageWithURL:[NSURL URLWithString:model.imgsrc] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
//                UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
//                //设置frame
//                button.frame = CGRectMake(imageX, imageY, imageW, imageH);
//                [button addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
//                [button setImage:image forState:UIControlStateNormal];
//                [scrollView addSubview:button];
//            }];
//            //标题
//            UIButton * titleBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//            titleBtn.frame = CGRectMake(i*320+10,195,200,15);
//            [titleBtn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
//            titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//            titleBtn.titleLabel.font = [UIFont systemFontOfSize:12];
//            [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//            [titleBtn setTitle:model.title forState:UIControlStateNormal];
//            [self.scrollView addSubview:titleBtn];
//            self.scrollView.showsHorizontalScrollIndicator = NO;
//        }
//        //2.设置scrollview的滚动范围
//        CGFloat contentW = totalCount *imageW;
//        //不允许在垂直方向上进行滚动
//        self.scrollView.contentSize = CGSizeMake(contentW, 0);
//        //3.设置分页
//        self.scrollView.pagingEnabled = YES;
//        //4.监听scrollview的滚动
//        self.scrollView.delegate = self;
//        [self addTimer];
    }
    else
    {
        //图集
        if (topNews.imgextra != nil) {
            //标题
            self.titleLabel.frame =CGRectMake(10, 6.75, 190, 20);
            self.titleLabel.font = [UIFont systemFontOfSize:14];
            self.titleLabel.text = topNews.title;
            //第一张图
            self.newsImageView.frame = CGRectMake(10, CGRectGetMaxY(self.titleLabel.frame) + 6.75, 99, 69.5);
            [self.newsImageView sd_setImageWithURL:[NSURL URLWithString:topNews.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"] options:SDWebImageProgressiveDownload];
            //第二和第三张图
            self.secondImageView.frame = CGRectMake(CGRectGetMaxX(self.newsImageView.frame) +2,  CGRectGetMaxY(self.titleLabel.frame) + 6.75, 99, 69.5);
            [self.secondImageView sd_setImageWithURL:[NSURL URLWithString:[topNews.imgextra[0] objectForKey:@"imgsrc"]]placeholderImage:[UIImage imageNamed:@"placeholderImage"] options:SDWebImageProgressiveDownload];
            self.thirdImageView.frame = CGRectMake(CGRectGetMaxX(self.secondImageView.frame) +2, CGRectGetMaxY(self.titleLabel.frame) + 6.75, 99, 69.5);
            [self.thirdImageView sd_setImageWithURL:[NSURL URLWithString:[topNews.imgextra [1] objectForKey:@"imgsrc"]]placeholderImage:[UIImage imageNamed:@"placeholderImage"] options:SDWebImageProgressiveDownload];
            //底部的线
               self.bottomLine.backgroundColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0];
            self.bottomLine.frame = CGRectMake(0,115.25, [UIScreen mainScreen].bounds.size.width, 0.75);
        }
        //独家
        else if(topNews.editor != nil){
            //图片
            self.newsImageView.frame = CGRectMake(10,35,300,90);
            [self.newsImageView sd_setImageWithURL:[NSURL URLWithString:topNews.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"] options:SDWebImageProgressiveDownload];
            //标题
            self.titleLabel.frame = CGRectMake(10, 9.75, 290, 20);
            self.titleLabel.text = topNews.title;
            self.titleLabel.font = [UIFont systemFontOfSize:14];
            //摘要
            self.digestLabel.frame = CGRectMake(10, 120, 290, 40);
            self.digestLabel.text = topNews.digest;
            self.digestLabel.numberOfLines = 0;
            self.digestLabel.font = [UIFont systemFontOfSize:12];
            //底部的线
            self.bottomLine.backgroundColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0];
            self.bottomLine.frame = CGRectMake(0, 164.25,[UIScreen mainScreen].bounds.size.width, 0.75);
        }
        //普通类型
        else{
            CGPoint tempoint  = CGPointMake(10,9.75);
            CGSize temsize = CGSizeMake(88.64, 65);
            //图片
            self.newsImageView.frame = (CGRect){tempoint,temsize};
            self.newsImageView.contentMode = UIViewContentModeScaleAspectFill;
            self.newsImageView.clipsToBounds = YES;
            [self.newsImageView sd_setImageWithURL:[NSURL URLWithString:topNews.imgsrc] placeholderImage:[UIImage imageNamed:@"placeholderImage"] options:SDWebImageProgressiveDownload];
            //标题
            self.titleLabel.frame = CGRectMake(CGRectGetMaxX(self.newsImageView.frame)+10,10,[UIScreen mainScreen].bounds.size.width, 20);
            self.titleLabel.text = topNews.title;
            self.titleLabel.font = [UIFont systemFontOfSize:14.5];
            //摘要
            self.digestLabel.frame = CGRectMake(self.titleLabel.frame.origin.x,CGRectGetMaxY(self.titleLabel.frame), [UIScreen mainScreen].bounds.size.width-10-CGRectGetMaxX(self.newsImageView.frame)-10,50);
            self.digestLabel.text = topNews.digest;
            self.digestLabel.numberOfLines = 0;
            self.digestLabel.lineBreakMode = NSLineBreakByCharWrapping;
            self.digestLabel.font = [UIFont systemFontOfSize:13];
            //底部的线
               self.bottomLine.backgroundColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1.0];
            self.bottomLine.frame = CGRectMake(0,84.25, [UIScreen mainScreen].bounds.size.width, 0.75);
        }
    }
}

- (void)btnClicked:(UIButton *)btn
{
    
}
- (void)nextImage
{
    int page = (int)self.pageControl.currentPage;
    if (page == 2) {
        page = 0;
    }else
    {
        page++;
    }
    //  滚动scrollview
    CGFloat x = page * self.scrollView.frame.size.width;
    self.scrollView.contentOffset = CGPointMake(x, 0);
}
// scrollview滚动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //    计算页码
    //    页码 = (contentoffset.x + scrollView一半宽度)/scrollView宽度
    CGFloat scrollviewW =  scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    int page = (x + scrollviewW / 2) /  scrollviewW;
    self.pageControl.currentPage = page;
}
// 开始拖拽的时候调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    //    关闭定时器(注意点; 定时器一旦被关闭,无法再开启)
    //    [self.timer invalidate];
    [self removeTimer];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    //    开启定时器
    [self addTimer];
}
//开启定时器
- (void)addTimer{
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(nextImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}
//关闭定时器
- (void)removeTimer
{
    [self.timer invalidate];
}




- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
